const express = require('express')
const cookieParser = require('cookie-parser')

const app = express()
app.use(cookieParser())


app.use('/', function(req, res, next) {
	res.set({
		"Access-Control-Allow-Origin": "*",
    "Access-Control-Allow-Headers": "X-Requested-With",
    "Access-Control-Allow-Methods": "PUT,POST,GET,DELETE,OPTIONS",
    'Access-Control-Allow-Credentials': 'true'
	})

	next()
})

app.get('/api/data', function(req, res) {
	res.json(
		{
	    "success": true,
	    "list": [
	      {
	        "id": 12,
	        "title": "Boy reseued after failing into los angeless sevage system"
	      },
	      {
	        "id": 2,
	        "title": "Independent chefs exchange ideas"
	      },
	      {
	        "id": 3,
	        "title": "Higrants bring atttention to US-Mexican Border Folicies"
	      }
	    ]
	  }
	 )
})

app.get('/api/translation', function(req, res) {
	res.json(
		{
	    "success": true,
	    "list": [
	      {
	        "id": 12,
	        "title": "男孩落入洛杉矶污水系统后获救"
	      },
	      {
	        "id": 2,
	        "title": "独立厨师交换意见"
	      },
	      {
	        "id": 3,
	        "title": "移民引起了美墨边境政策的关注"
	      }
	    ]
	  }
	 )
})

app.get('/api/isLogin', function(req, res, next) {
	console.log(req.cookies)
	req.cookies && (req.cookies.login === 'true' ?  res.json(
			{
				'success': true,
				'data': {
					'login': true
				}
			}
		) : res.json(
			{
				'success': true,
				'data': {
					'login': false
				}
			}
		))
})


app.get('/api/login', function(req, res, next) {
	res.set({
		'Set-Cookie': 'login=true;Domain=http://localhost:3000'
	})
	res.json(
		{
			'success': true,
			'data': {
				login: true
			}
		}
	)
})

app.get('/api/logout', function(req, res, next) {
	res.set({
		'Set-Cookie': 'login='
	})
	res.json(
		{
			'success': true,
			'data': {
				login: false
			}
		}
	)
})

app.listen(8000, function() {
	console.log('server is run in http://localhost:8000')
})